package com.example.allplan.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.JiHuaJavaBean;
import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.JavaBean.JiHuaXinXiJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.MyDialog.ShuRuJiHuaDialog;
import com.example.allplan.MyView.MyLinearLayout;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.JiHuaTiaoRecyclerAdapter;
import com.example.allplan.ShuJuKuCaoZuo.JiHuaSQCaoZuo;

import java.util.ArrayList;
import java.util.Collections;

public class BianJiJiHuaActivity extends BaseActivity implements View.OnClickListener {
   Toolbar mtoolbar;
   TextView mleixing;
   int leixing,idyoubiao,suoshuid,baohanrenwushu,jihuazhongid;
   RecyclerView mrecycler;
   JiHuaSQCaoZuo mji_hua_sqcaozuo;
   JiHuaXinXiJavaBean mxinxi;
   ArrayList<JiHuaTiaoJavaBean> mlist;
   JiHuaTiaoRecyclerAdapter madapter;
   MyLinearLayout mgen;
   MyApplication myapp;
   EditText shu_ru_biao_ti_edittext;
   String wenjianming,biaoti;
   boolean shifoufuzhi;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bian_ji_ji_hua_activity);
        init();
    }
    void init(){
        shu_ru_biao_ti_edittext=findViewById(R.id.shu_ru_ji_hua_ming_edittext);
        suoshuid=getIntent().getIntExtra("suo_shu_id",-1);
        baohanrenwushu=getIntent().getIntExtra("bao_han_ren_wu_shu",-1);
        wenjianming=getIntent().getStringExtra("wen_jian_ming");
        shifoufuzhi=getIntent().getBooleanExtra("fu_zhi",false);
        myapp=MyApplication.getInstance();
        mgen=findViewById(R.id.bian_ji_ji_hua_gen);
        mtoolbar=findViewById(R.id.bian_ji_yi_ge_ji_hua_toolbar);
        setSupportActionBar(mtoolbar);
        mleixing=findViewById(R.id.xuan_ze_lei_xing_textview);
        mleixing.setOnClickListener(this);
        mrecycler=findViewById(R.id.ji_hua_tiao_mu_recyclerview);
        mlist=new ArrayList<JiHuaTiaoJavaBean>();
        if(wenjianming!=null&&!shifoufuzhi){
            mji_hua_sqcaozuo=new JiHuaSQCaoZuo(this,wenjianming);
            mlist=mji_hua_sqcaozuo.reloadAllJiHuaTiao();
            mxinxi=mji_hua_sqcaozuo.du_qu_xinxi();
            leixing=mxinxi.getLei_xing();
            idyoubiao=mxinxi.getId_you_biao();
            jihuazhongid=mxinxi.getJi_hua_id();
            biaoti=mxinxi.getBiao_ti();
            shu_ru_biao_ti_edittext.setText(biaoti);
            Collections.sort(mlist);
            setLeixing(leixing);
        }else if(wenjianming==null&&suoshuid!=-1&&baohanrenwushu!=-1&&!shifoufuzhi){
            idyoubiao=0;
            mlist.add(new JiHuaTiaoJavaBean(idyoubiao++,mlist.size()));
            mlist.add(new JiHuaTiaoJavaBean(idyoubiao++,mlist.size()));
        }else if(wenjianming!=null&&shifoufuzhi){
            mji_hua_sqcaozuo=new JiHuaSQCaoZuo(this,wenjianming);
            mlist=mji_hua_sqcaozuo.reloadAllJiHuaTiao();
            mxinxi=mji_hua_sqcaozuo.du_qu_xinxi();
            leixing=mxinxi.getLei_xing();
            idyoubiao=mxinxi.getId_you_biao();
            biaoti=mxinxi.getBiao_ti();
            shu_ru_biao_ti_edittext.setText(biaoti);
            Collections.sort(mlist);
            setLeixing(leixing);
        }

        madapter=new JiHuaTiaoRecyclerAdapter(mlist, new JiHuaTiaoRecyclerAdapter.onItemClickListener() {
            @Override
            public void onClick(final int pos, Context mcon) {
                ShuRuJiHuaDialog mshurudialog=ShuRuJiHuaDialog.newInstance();
                mshurudialog.mshuru=mlist.get(pos);
                mshurudialog.setOnFinish(new ShuRuJiHuaDialog.onfinish() {
                    @Override
                    public void onFinish(JiHuaTiaoJavaBean o) {
                        mlist.set(pos,o);
                        madapter.notifyItemChanged(pos);

                    }
                });
                mshurudialog.show(getSupportFragmentManager(),"edit");

            }
        },this);
        mrecycler.setAdapter(madapter);
        mrecycler.setLayoutManager(new LinearLayoutManager(this));
        JiHuaBaoTouchCallback mcallback=new JiHuaBaoTouchCallback(new JiHuaBaoTouchCallback.itemtouchhelpercallback() {
            @Override
            public void onitemmove(int frompos, int topos) {
                int temp = mlist.get(frompos).getPai_xu_you_biao();
                mlist.get(frompos).setPai_xu_you_biao(mlist.get(topos).getPai_xu_you_biao());
                mlist.get(topos).setPai_xu_you_biao(temp);
                Collections.swap(mlist, frompos, topos);
                madapter.notifyItemMoved(frompos, topos);
            }

            @Override
            public void onitemswipe(int direction) {
                mlist.remove(direction);
                // Log.d("kaishi",System.currentTimeMillis()+"");
                for(int i=direction;i<mlist.size();i++){
                    mlist.get(i).setPai_xu_you_biao(mlist.get(i).getPai_xu_you_biao()-1);
                }
                madapter.notifyItemRemoved(direction);
                //Log.d("kaishi1",System.currentTimeMillis()+"");
                mgen.setCantclick(true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(450);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //madapter.notifyDataSetChanged();
                                mgen.setCantclick(false);
                                //Log.d("kaishi2",System.currentTimeMillis()+"");
                            }
                        });
                    }
                }).start();
            }
        });
        ItemTouchHelper mhelper=new ItemTouchHelper(mcallback);
        mhelper.attachToRecyclerView(mrecycler);
     }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.bian_ji_ji_hua_menu,menu);
        return true;
    }
    boolean check(){
        if(shu_ru_biao_ti_edittext.getText().toString().equals("")){
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            return false;
        }else if(mlist.size()==0){
            Toast.makeText(this, "计划不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }else{
            int sum=0;
            for(int i=0;i<mlist.size();i++){
                if(mlist.get(i).getChu_shi_zhi()==0){
                    Toast.makeText(this, "计划时间不能为0", Toast.LENGTH_SHORT).show();
                    return false;
                }
                sum+=mlist.get(i).getChu_shi_zhi();
            }
            if(leixing==0&&sum>168){
                Toast.makeText(this, "一周只有168小时", Toast.LENGTH_SHORT).show();
                return false;
            }else if(leixing==1&&sum>24){
                Toast.makeText(this, "一天只有24小时", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }
    void savenew(){
        mji_hua_sqcaozuo=new JiHuaSQCaoZuo(this,"plan"+myapp.ji_hua_wen_jian_xia_biao);
        myapp.mSuoYouJiHuaSQCaoZuo.write_one(new JiHuaJavaBean(myapp.ji_hua_wen_jian_xia_biao,leixing,suoshuid,baohanrenwushu,suoshuid,shu_ru_biao_ti_edittext.getText().toString(),"plan"+myapp.ji_hua_wen_jian_xia_biao));
        mji_hua_sqcaozuo.write_shu_zu(mlist);
        mji_hua_sqcaozuo.xie_ru_xin_xi(new JiHuaXinXiJavaBean(idyoubiao,leixing,0,myapp.ji_hua_wen_jian_xia_biao,"plan"+myapp.ji_hua_wen_jian_xia_biao,shu_ru_biao_ti_edittext.getText().toString()));
        myapp.putSH_Int(myapp.JI_HUA_WEN_JIAN_XIA_BIAO_SHARE_NAME,++myapp.ji_hua_wen_jian_xia_biao);
        myapp.putSH_Int(myapp.JI_HUA_TIAO_SHU_SHARE_NAME,++myapp.ji_hua_tiao_shu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.bian_ji_yi_ge_ji_hua_wan_cheng:
                if(check()){
                    if(wenjianming==null){
                        savenew();
                        Intent min=new Intent();
                        min.putExtra("xuan_zhong_id",suoshuid);
                        setResult(32,min);
                        finish();
                    }else if(wenjianming!=null&&!shifoufuzhi){
                        myapp.mSuoYouJiHuaSQCaoZuo.update_one_only_three(new JiHuaJavaBean(jihuazhongid,leixing,suoshuid,baohanrenwushu,suoshuid,shu_ru_biao_ti_edittext.getText().toString(),wenjianming));
                        mji_hua_sqcaozuo.delete_suo_you_chuang_jian();
                        mji_hua_sqcaozuo.write_shu_zu(mlist);
                        mji_hua_sqcaozuo.geng_xin_xin_xi_only_three(new JiHuaXinXiJavaBean(idyoubiao,leixing,0,jihuazhongid,wenjianming,shu_ru_biao_ti_edittext.getText().toString()));
                        setResult(52);
                        finish();
                    }else if(wenjianming!=null&&shifoufuzhi){
                        savenew();
                        setResult(62);
                        finish();
                    }

                }
                break;
            case R.id.tian_jia_yi_ge_ji_hua_item:
                mlist.add(new JiHuaTiaoJavaBean(idyoubiao++,mlist.size()));
                madapter.notifyDataSetChanged();
                break;
            case R.id.tong_ji_shi_jian:
                Toast.makeText(this, ""+tongjishijian(), Toast.LENGTH_SHORT).show();
                break;


        }
        return true;
    }
    int tongjishijian(){
        int sum=0;
        for(int i=0;i<mlist.size();i++){
            sum+=mlist.get(i).getChu_shi_zhi();
        }
        return sum;
    }
    void setLeixing(int n){
        if(n==0){
            leixing=0;
            mleixing.setText("周");
        }else if(n==1){
            leixing=1;
            mleixing.setText("天");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.xuan_ze_lei_xing_textview:
                if(leixing==0){
                    setLeixing(1);
                }else if(leixing==1){
                    setLeixing(0);
                }
                break;
        }
    }
}
